package com.guogz.pic.sevice.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.guogz.pic.sevice.dto.ErrorCode;
import com.guogz.pic.sevice.dto.ResponseData;
import com.guogz.pic.sevice.service.UploadService;

/**
 * @类描述： 上传图片接口服务
 * 
 * @类名称：UploadController
 * @创建人：GuoGuanzhi
 * @创建时间：2018年10月10日上午10:30:27
 * @mail Hunter909683502@126.com
 */
@RestController("/")
public class UploadController {
	private Logger logger = LoggerFactory.getLogger(UploadController.class);

	@Value("${images.upload.path}")
	private String uploadPath;

	@Autowired
	UploadService uploadService;

	
	/**
	 * @throws ServletException @throws IOException @描述: 对外提供上传服务 @方法名:
	 * uploadService @param multipartFileList @param request @return @返回类型
	 * ResponseData<List<String>> @创建人 GuoGuanzhi @创建时间 2018年10月10日下午3:15:53 @throws
	 */
	@PostMapping("/uploadService")
	@ResponseBody
	public ResponseData<List<String>> uploadService(HttpServletRequest request) throws IOException, ServletException {

		logger.info("客户端：{} 开始上传！", request.getRemoteAddr());
		
		MultipartResolver multipartResolver = new CommonsMultipartResolver(request.getServletContext());

		// 判断是否是多文件请求
		if (!multipartResolver.isMultipart(request)) {
			logger.warn("非上传文件的请求！ip:{}", request.getRemoteAddr());
			return ResponseData.error(ErrorCode.UPLOAD_ERROR_REQUEST);
		}

		int fileSize = request.getParts().size();

		// 调用服务处理
		List<String> pathList = uploadService.uploadPic(request, uploadPath);

		logger.info("上传的结果是：{}", pathList);
		// 图片上传不完整或者上传失败
		if (pathList.size() != fileSize) {
			logger.error("图片上传失败，总需要上传{} 张，上传了{} 张", fileSize, pathList.size());
			return ResponseData.error(ErrorCode.UPLOAD_ERROR_SERVER);
		}

		return ResponseData.success(pathList);
	}
	
	@PostMapping("/uploadGetList")
	@ResponseBody
	public List<String> uploadServiceList(HttpServletRequest request) throws IOException, ServletException {

		logger.info("客户端：{} 开始上传！", request.getRemoteAddr());
		
		MultipartResolver multipartResolver = new CommonsMultipartResolver(request.getServletContext());

		// 判断是否是多文件请求
		if (!multipartResolver.isMultipart(request)) {
			logger.warn("非上传文件的请求！ip:{}", request.getRemoteAddr());
			return null;
		}

		int fileSize = request.getParts().size();

		// 调用服务处理
		List<String> pathList = uploadService.uploadPic(request, uploadPath);

		logger.info("上传的结果是：{}", pathList);
		// 图片上传不完整或者上传失败
		if (pathList.size() != fileSize) {
			logger.error("图片上传失败，总需要上传{} 张，上传了{} 张", fileSize, pathList.size());
			return null;
		}

		return pathList;
	}

}
